package cn.edu.xust.service.impl;


import cn.edu.xust.configuration.exception.Default200Exception;
import cn.edu.xust.configuration.exception.ErrorConstant;
import cn.edu.xust.dao.SysRoleDao;
import cn.edu.xust.entity.SysRoleEntity;
import cn.edu.xust.service.SysRoleService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                new QueryWrapper<SysRoleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysRoleEntity> getByRolids(List<Integer> roleids) throws Default200Exception {
        if(roleids.size()==0){
            throw new Default200Exception(ErrorConstant.role_is_empty);
        }
        QueryWrapper<SysRoleEntity> wap = new QueryWrapper<SysRoleEntity>().in("roleid", roleids);
        return baseMapper.selectList(wap);
    }


    @Override
    public SysRoleEntity getByCname(String cname) {

        QueryWrapper<SysRoleEntity> wap = new QueryWrapper<SysRoleEntity>().eq("cname", cname);
        return baseMapper.selectOne(wap);

    }

    @Override
    public SysRoleEntity getByNameAndId(String cname, Integer roleid) {
        QueryWrapper<SysRoleEntity> wap = new QueryWrapper<SysRoleEntity>().eq("cname", cname).eq("roleid", roleid);
        return baseMapper.selectOne(wap);


    }

    @Override
    public boolean updatename(SysRoleEntity sysRoleEntity) {
        UpdateWrapper<SysRoleEntity> wap = new UpdateWrapper<SysRoleEntity>().eq("roleid", sysRoleEntity.getRoleid());
        wap.set("cname", sysRoleEntity.getCname());
        return baseMapper.update(null, wap) == 1;

    }

    @Override
    public List<SysRoleEntity> getByMakerids(List<Integer> makerids) {

        QueryWrapper<SysRoleEntity> wap = new QueryWrapper<SysRoleEntity>().or().in("makerid", makerids);
        return baseMapper.selectList(wap);
    }

    @Override
    public List<SysRoleEntity> getByMakerid(Integer uid) {
        QueryWrapper<SysRoleEntity> wap = new QueryWrapper<SysRoleEntity>().or().eq("makerid", uid);
        return baseMapper.selectList(wap);
    }



    @Override
    public boolean updateshareroles(SysRoleEntity sysRoleEntity) {

        UpdateWrapper<SysRoleEntity> wap = new UpdateWrapper<SysRoleEntity>().eq("roleid", sysRoleEntity.getRoleid());
        wap.set("shareroles", sysRoleEntity.getShareroles());
        return baseMapper.update(null, wap) == 1;


    }


}